﻿using System.Collections.Generic;
using System.Linq;
using yiCommerce.Core.Domain.Pictures;
using yiCommerce.Core.Domain.Products;

namespace yiCommerce.Service.Products
{
    public class ProductMapper
    {
        public static IList<Product> ToProducts(IList<Product> products)
        {
            return products.Select(ToProduct).ToList();
        }

        public static ProductVariant ToProductVariant(ProductVariant p)
        {
              return new ProductVariant()
                {
                    Id = p.Id,
                    ProductId = p.ProductId,
                   
                    Name = p.Name,
                    Sku = p.Sku,
                    Price  = p.Price,
                    DisplayOrder = p.DisplayOrder,
                    StockQuantity = p.StockQuantity,
                    Product = new Product()
                        {
                            Deleted = p.Product.Deleted,
                            Published = p.Product.Published,
                            Id =p.Product.Id,
                            ProductName = p.Product.ProductName,
                            ShortDescription = p.Product.ShortDescription,
                            FullDescription = p.Product.FullDescription,
                        },

                    ProductVariantAttributes = ToProductVariantAttributes(p.ProductVariantAttributes),
                    Published = p.Published,
                    Deleted = p.Deleted,
                    PictureId = p.PictureId,
                };
        }

        public static Product ToProduct(Product p)
        {
            return new Product()
            {
                Id = p.Id,
                ProductName = p.ProductName,
                ShortDescription = p.ShortDescription,
                FullDescription = p.FullDescription,
                ProductPictures = ToProductPictures(p.ProductPictures),
                ProductVariants = ToProductVariants(p.ProductVariants),
                ProductSpecificationAttributes = ToProductSpecificationAttributes(p.ProductSpecificationAttributes),
                Deleted = p.Deleted,
                Published = p.Published,
            };
        }


        private static ICollection<ProductSpecificationAttribute> ToProductSpecificationAttributes(ICollection<ProductSpecificationAttribute> productSpecificationAttributes)
        {
            if (productSpecificationAttributes == null)
            {
                return new List<ProductSpecificationAttribute>();
            }
            else
            {
                return productSpecificationAttributes.Select(c => new ProductSpecificationAttribute()
                {
                    Id = c.Id,

                    SpecificationAttributeOption = new SpecificationAttributeOption()
                    {
                        Id = c.SpecificationAttributeOption.Id,
                        Name = c.SpecificationAttributeOption.Name,
                        SpecificationAttribute = new SpecificationAttribute()
                        {
                            Id = c.SpecificationAttributeOption.SpecificationAttribute.Id,
                            Name = c.SpecificationAttributeOption.SpecificationAttribute.Name
                        }
                    }

                }).ToList();
            }
        }

        private static ICollection<ProductVariant> ToProductVariants(ICollection<ProductVariant> productVariants)
        {
            if (productVariants == null)
            {
                return new List<ProductVariant>();
            }
            else
            {
                return productVariants.Where(c=>!c.Deleted).Select(c => new ProductVariant()
                {
                    Id = c.Id,
                    Name = c.Name,
                    Sku = c.Sku,
                    Price = c.Price,
                    DisplayOrder = c.DisplayOrder,
                    Published = c.Published,
                    ProductVariantAttributes = ToProductVariantAttributes(c.ProductVariantAttributes),
                    StockQuantity = c.StockQuantity,
                }).ToList();
            }
        }

        private static ICollection<ProductVariantAttribute> ToProductVariantAttributes(ICollection<ProductVariantAttribute> productVariantAttributes)
        {
            if (productVariantAttributes == null)
            {
                return new List<ProductVariantAttribute>();
            }
            else
            {
                return productVariantAttributes.Select(c => new ProductVariantAttribute()
                {
                    Id = c.Id,
                    ProductAttribute = new ProductAttribute()
                    {
                        Id = c.ProductAttribute.Id,
                        Description = c.ProductAttribute.Description,
                        Name = c.ProductAttribute.Name
                    },
                    AttributeControlType = c.AttributeControlType,

                    ProductVariantAttributeValues = c.ProductVariantAttributeValues.Select(t => new ProductVariantAttributeValue()
                    {
                        DisplayOrder = t.DisplayOrder,
                        Name = t.Name,
                        Id = t.Id,
                        ProductVariantAttributeId = t.ProductVariantAttributeId
                    }).ToList()

                }).ToList();
            }

        }

        private static IList<ProductPicture> ToProductPictures(ICollection<ProductPicture> productPictures)
        {
            if (productPictures == null)
            {
                return new List<ProductPicture>();
            }
            else
            {
                return productPictures.Select(c => new ProductPicture()
                {
                    PictureId =  c.PictureId,
                    ProductId = c.ProductId,
                    Id = c.Id,
                    DisplayOrder = c.DisplayOrder,
                    Picture = new Picture() { Id = c.Picture.Id }
                }).ToList();
            }


        }

       
    }
}